/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import bean.Tema;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fernando
 */
public class TemaDao implements Serializable {

    private Conexao conexao;
    private PreparedStatement ps;
    private boolean sucess = false;

    public TemaDao() {
        this.conexao = new ConexaoPostgres();
    }

    public boolean validarCadastroTema(String tema) {
        try {
            ps = conexao.getConnection().prepareStatement("SELECT * FROM tema WHERE tema = ?");
            ps.setString(1, tema);
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();

            if (rs.next()) {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return false;
        }
        return true;

    }

    public boolean cadastrarTema(Tema tema) {
        sucess = false;
        try {
            if (this.validarCadastroTema(tema.getTema())) {
                System.out.println("cadastro Tema: " + tema.getTema());
                ps = conexao.getConnection().prepareStatement("INSERT INTO TEMA(tema) VALUES (?) ");
                ps.setString(1, tema.getTema());

                ps.executeUpdate();
                conexao.closeConnection();

                sucess = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return sucess;
        }
        conexao.closeConnection();
        return sucess;

    }

    public boolean alterarTema(Tema tema) {
        sucess = false;


        try {

            ps = conexao.getConnection().prepareStatement("UPDATE TEMA SET tema = ? where id = ?");

            ps.setString(1, tema.getTema());
            ps.setInt(2, tema.getId());
            ps.executeUpdate();
            conexao.closeConnection();

            sucess = true;

        } catch (SQLException ex) {
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return sucess;
        }

        conexao.closeConnection();
        return sucess;
    }

    public List<Tema> listarTemas() {
        List<Tema> temas = new ArrayList<Tema>();
        try {
            ps = conexao.getConnection().prepareStatement("SELECT * FROM tema ORDER BY tema");
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();

            while (rs.next()) {
                Tema t = new Tema();

                t.setTema(rs.getString("tema"));
                t.setId(rs.getInt("id"));

                temas.add(t);


            }
        } catch (SQLException ex) {
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
        }

        conexao.closeConnection();
        return temas;

    }

    public Tema consultarTema(int id) {
        PreparedStatement ps = null;
        Tema t = null;
        try {
            t = new Tema();
            ps = conexao.getConnection().prepareStatement("Select * from tema where id = ?; ");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();
            if (rs.next()) {

                t.setTema(rs.getString("tema"));
                t.setId(id);


            }
        } catch (SQLException ex) {
            conexao.closeConnection();
            return t;

        }
        conexao.closeConnection();
        return t;
    }

    public boolean removerTema(Tema tema) {
        sucess = false;
        try {

            ps = conexao.getConnection().prepareStatement(" DELETE FROM tema WHERE id = ?");

            ps.setLong(1, (tema.getId()));

            ps.executeUpdate();
            conexao.closeConnection();


            sucess = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            conexao.closeConnection();
            return sucess;
        }

        conexao.closeConnection();
        return sucess;
    }
}
